package com.david.musicapp.activity

import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.david.musicapp.bean.Song
import com.david.musicapp.utils.SongHelper
import com.david.core.normalreq.UrlHttpRequest
import com.david.core.normalreq.UrlHttpResponse
import com.david.musicapp.R
import org.json.JSONObject
import java.util.*

class CloudSongActivity:Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cloud_songs)
        UrlHttpRequest().getRequest("http://load_star.gitee.io/htmlminifuncs/jsondata/musices" ,"", object :
            UrlHttpResponse{
            override fun onSuccess(result: String) {
                Log.e("result", result)
                val json = JSONObject(result)
                val arrays = json.getJSONArray("data")
                val resultList = ArrayList<Song>()
                for (index in 0 until arrays.length()) {
                    val item = arrays.getJSONObject(index)
                    resultList.add(Song(item.getString("title"),
                        item.getString("singer"),
                        item.getString("size"),
                        item.getInt("duration"),
                        item.getString("url"),
                        item.getLong("id")))
                }

                SongHelper.getInstance().cloudSongs = resultList
            }

            override fun onFail(msg: String) {
                Log.e("result", msg)
            }

        })
//        test.setOnClickListener {
//            val interFace = MyRetrofitHelper.getInstance().retrofit.create(IMusicListMethod::class.java)
//
//            val flowable = interFace.getMusics()
//
//            flowable.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(object : RetrofitBaseSubscriber() {
//                    override fun onSuccess(responseStr: String?) {
//                        Log.e("retrofit", "onSuccess: " + responseStr)
////                    getJsonObject(responseStr)
//                    }
//
//                    override fun onFail(throwable: Throwable?) {
//                        throwable?.printStackTrace()
//                    }
//
////                override fun getJavaObject(response: String?): MyRetrofitResponse {
////                    return JSON.parseObject(response, MyRetrofitResponse::class.java)
////                }
//                })
//        }

    }
}